const Product = require('../models/product.model');
const mongoose = require('mongoose');

// 获取所有产品
exports.getAllProducts = async (req, res) => {
  try {
    const {
      keyword = '',
      category = '',
      page = 1,
      limit = 10,
      sort = '-createdAt',
      minPrice,
      maxPrice,
      brand = '',
      inStock,
      featured
    } = req.query;

    // 构建查询条件
    const query = {};

    // 关键词搜索 - 使用更灵活的搜索方式
    if (keyword) {
      query.$or = [
        { name: { $regex: keyword, $options: 'i' } },
        { description: { $regex: keyword, $options: 'i' } },
        { brand: { $regex: keyword, $options: 'i' } },
        { category: { $regex: keyword, $options: 'i' } },
        { tags: { $in: [new RegExp(keyword, 'i')] } }
      ];
    }

    // 分类过滤
    if (category) {
      query.category = category;
    }

    // 品牌过滤
    if (brand) {
      query.brand = brand;
    }

    // 价格过滤
    if (minPrice !== undefined || maxPrice !== undefined) {
      query.price = {};
      if (minPrice !== undefined) query.price.$gte = Number(minPrice);
      if (maxPrice !== undefined) query.price.$lte = Number(maxPrice);
    }

    // 库存过滤
    if (inStock === 'true') {
      query.countInStock = { $gt: 0 };
    }

    // 特色产品
    if (featured === 'true') {
      query.isFeatured = true;
    }

    // 分页
    const pageNumber = parseInt(page);
    const limitNumber = parseInt(limit);
    const skip = (pageNumber - 1) * limitNumber;

    // 查询产品
    const products = await Product.find(query)
      .populate('shop', 'name logo type rating certification')
      .sort(sort)
      .skip(skip)
      .limit(limitNumber);

    // 总数量
    const totalProducts = await Product.countDocuments(query);

    res.json({
      products,
      page: pageNumber,
      pages: Math.ceil(totalProducts / limitNumber),
      total: totalProducts,
    });
  } catch (error) {
    res.status(500).json({ message: '获取产品列表失败', error: error.message });
  }
};

// 获取单个产品详情
exports.getProductById = async (req, res) => {
  try {
    const productId = req.params.id;
    console.log('正在查找商品，ID:', productId);

    // 检查ID格式
    if (!productId || !mongoose.Types.ObjectId.isValid(productId)) {
      console.log('无效的商品ID格式:', productId);
      return res.status(400).json({
        message: '无效的商品ID格式',
        receivedId: productId
      });
    }

    const product = await Product.findById(productId)
      .populate('shop', 'name description logo banner type categories rating stats certification');

    if (!product) {
      console.log('商品未找到，ID:', productId);
      return res.status(404).json({
        message: '商品未找到',
        searchedId: productId
      });
    }

    console.log('商品查找成功:', product.name);
    res.json(product);
  } catch (error) {
    console.error('获取商品详情失败:', error);
    res.status(500).json({
      message: '获取商品详情失败',
      error: error.message,
      requestedId: req.params.id
    });
  }
};

// 创建产品评论
exports.createProductReview = async (req, res) => {
  try {
    const { rating, comment } = req.body;
    const productId = req.params.id;

    // 验证评论数据
    if (!rating || !comment) {
      return res.status(400).json({ message: '请提供评分和评论内容' });
    }

    const product = await Product.findById(productId);

    if (!product) {
      return res.status(404).json({ message: '产品未找到' });
    }

    // 假设用户信息从身份验证中间件中获取
    const user = req.user;

    // 检查用户是否已经评论过该产品
    const alreadyReviewed = product.reviews.find(
      (r) => r.user.toString() === user._id.toString()
    );

    if (alreadyReviewed) {
      return res.status(400).json({ message: '您已经评论过该产品' });
    }

    // 创建新评论
    const review = {
      name: user.name,
      rating: Number(rating),
      comment,
      user: user._id,
    };

    // 添加到评论数组
    product.reviews.push(review);

    // 更新评分统计信息
    product.numReviews = product.reviews.length;
    product.rating = product.reviews.reduce((acc, item) => item.rating + acc, 0) / product.reviews.length;

    // 保存产品
    await product.save();

    res.status(201).json({ message: '评论添加成功', review });
  } catch (error) {
    res.status(500).json({ message: '添加评论失败', error: error.message });
  }
};

// 获取推荐产品
exports.getRecommendedProducts = async (req, res) => {
  try {
    const { category, limit = 6 } = req.query;
    const productId = req.params.id;

    // 获取相同分类的产品，排除当前产品
    const recommendedProducts = await Product.find({
      _id: { $ne: productId },
      ...(category ? { category } : {}),
    })
      .sort({ salesCount: -1, rating: -1 })
      .limit(parseInt(limit));

    res.json(recommendedProducts);
  } catch (error) {
    res.status(500).json({ message: '获取推荐产品失败', error: error.message });
  }
};

// 获取热门分类
exports.getTopCategories = async (req, res) => {
  try {
    // 聚合产品，按分类分组并计算每个分类的产品数量
    const categories = await Product.aggregate([
      { $group: { _id: '$category', count: { $sum: 1 } } },
      { $sort: { count: -1 } },
      { $limit: 10 }
    ]);

    res.json(categories);
  } catch (error) {
    res.status(500).json({ message: '获取热门分类失败', error: error.message });
  }
};

// 添加获取轮播图API
exports.getBanners = async (req, res) => {
  try {
    // 先尝试获取精选产品作为轮播图
    let featuredProducts = await Product.find({ isFeatured: true })
      .select('name mainImage images')
      .limit(5);

    // 如果没有精选产品，获取前5个有图片的产品
    if (!featuredProducts || featuredProducts.length === 0) {
      featuredProducts = await Product.find({
        $or: [
          { mainImage: { $exists: true, $ne: null, $ne: '' } },
          { images: { $exists: true, $ne: [], $ne: null } }
        ]
      })
        .select('name mainImage images')
        .limit(5);
    }

    // 如果还是没有产品，返回默认轮播图
    if (!featuredProducts || featuredProducts.length === 0) {
      const defaultBanners = [
        {
          id: 'default1',
          title: '欢迎来到小米商城',
          image: 'https://via.placeholder.com/800x400/FF6900/FFFFFF?text=小米商城'
        },
        {
          id: 'default2',
          title: '精选好物',
          image: 'https://via.placeholder.com/800x400/1976D2/FFFFFF?text=精选好物'
        }
      ];
      return res.json(defaultBanners);
    }

    // 转换为轮播图格式
    const banners = featuredProducts.map(product => {
      // 确保图片URL是有效的
      let imageUrl = null;
      if (product.mainImage) {
        imageUrl = product.mainImage;
      } else if (product.images && product.images.length > 0) {
        imageUrl = product.images[0];
      }

      return {
        id: product._id.toString(),
        title: product.name,
        image: imageUrl || 'https://via.placeholder.com/800x400/CCCCCC/FFFFFF?text=' + encodeURIComponent(product.name)
      };
    });

    res.json(banners);
  } catch (err) {
    console.error('获取轮播图失败:', err);
    // 即使出错也返回默认轮播图，避免前端报错
    const defaultBanners = [
      {
        id: 'error1',
        title: '小米商城',
        image: 'https://via.placeholder.com/800x400/FF6900/FFFFFF?text=小米商城'
      }
    ];
    res.json(defaultBanners);
  }
};